﻿using System;
using System.Collections.Generic;
using BCCommon.Extensions;
using BCEntity.OA.Attendance.AttendanceRemedyRules;
using BCData.OA.Attendance.AttendanceRemedyRules;
using BCDto.MySql.Admin;
using BCDto.OA.Attendance.AttendanceRemedyRules;
using BCService.Utils;

namespace BCService.OA.Attendance.AttendanceRemedyRules
{
    /// <summary>
    /// 补卡规则
    /// </summary>
    public class AttendanceRemedyRulesService : IAttendanceRemedyRulesService
    {
        private readonly IAttendanceRemedyRulesData attendanceRemedyRulesData;

        public AttendanceRemedyRulesService(IAttendanceRemedyRulesData attendanceRemedyRulesData)
        {
            this.attendanceRemedyRulesData = attendanceRemedyRulesData;
        }

        /// <summary>
        /// 新创建补卡规则信息
        /// </summary>
        /// <param name="admin">当前登录管理员</param>
        /// <param name="dto">请求的补卡规则信息</param>
        /// <returns></returns>

        public AttendanceRemedyRulesDto Create(AdminDto admin, AttendanceRemedyRulesRequestDto dto)
        {
            var entity = dto.As<AttendanceRemedyRulesEntity>();
            entity.ScopeRange = "全公司";
            entity.AdminId = admin.AdminId;
            entity.AdminName = admin.FullName;
            if (dto.IsAllow.IsNotNull() && dto.IsAllow)
            {
                if (dto.LimitRemedyCount <= 0)
                {
                    throw new ArgumentException("限制补卡次数不能为空");
                }
                if (dto.LimitRemedyDays <= 0)
                {
                    throw new ArgumentException("限制补卡时间不能为空");
                }
                entity.Content = string.Format("允许补卡-每月可提交{0}次补卡-可申请过去{1}天内的补卡", dto.LimitRemedyCount, dto.LimitRemedyDays);
            }
            else
            {
                entity.Content = string.Format("不允许补卡");
            }
            entity.LimitRemedyCount = dto.LimitRemedyCount > 0 ? dto.LimitRemedyCount : 0;
            entity.LimitRemedyDays = dto.LimitRemedyDays > 0 ? dto.LimitRemedyDays : 0;
            return attendanceRemedyRulesData.InsertAsync(entity).GetAwaiter().GetResult().As<AttendanceRemedyRulesDto>();
        }


        /// <summary>
        /// 获取补卡规则信息列表
        /// </summary>
        /// <returns></returns>
        public List<AttendanceRemedyRulesDto> GetList()
        {
            return attendanceRemedyRulesData.GetAsync().GetAwaiter().GetResult().As<List<AttendanceRemedyRulesDto>>();
        }


        /// <summary>
        /// 获取补卡规则信息
        /// </summary>
        /// <returns></returns>
        public AttendanceRemedyRulesDto GetFirstDefault()
        {
            return attendanceRemedyRulesData.GetFirstDefaultAsync().GetAwaiter().GetResult().As<AttendanceRemedyRulesDto>();
        }


        /// <summary>
        /// 获取补卡规则信息
        /// </summary>
        /// <param name="remedyRuleId">补卡规则Id</param>
        /// <returns></returns>
        public AttendanceRemedyRulesDto GetById(long remedyRuleId)
        {
            return attendanceRemedyRulesData.GetByIdAsync(remedyRuleId).GetAwaiter().GetResult().As<AttendanceRemedyRulesDto>();
        }

        /// <summary>
        /// 修改指定的补卡规则信息
        /// </summary>
        /// <param name="admin">当前管理员</param>
        /// <param name="remedyRuleId">补卡规则Id</param>
        /// <param name="dto">修改信息</param>
        /// <returns></returns>
        public AttendanceRemedyRulesDto Update(AdminDto admin, long remedyRuleId, AttendanceRemedyRulesRequestDto dto)
        {
            var entity = dto.As<AttendanceRemedyRulesEntity>();
            entity.ModifierId = admin.AdminId;
            entity.RemedyRuleId = remedyRuleId;
            entity.ModifierName = admin.FullName;
            if (dto.IsAllow.IsNotNull() && dto.IsAllow)
            {
                if (dto.LimitRemedyCount <= 0)
                {
                    throw new ArgumentException("限制补卡次数不能为空");
                }
                if (dto.LimitRemedyDays <= 0)
                {
                    throw new ArgumentException("限制补卡时间不能为空");
                }
                entity.Content = string.Format("允许补卡-每月可提交{0}次补卡-可申请过去{1}天内的补卡", dto.LimitRemedyCount, dto.LimitRemedyDays);
            }
            else
            {
                entity.Content = string.Format("不允许补卡");
            }
            entity.LimitRemedyCount = dto.LimitRemedyCount > 0 ? dto.LimitRemedyCount : 0;
            entity.LimitRemedyDays = dto.LimitRemedyDays > 0 ? dto.LimitRemedyDays : 0;
            return attendanceRemedyRulesData.UpdateAsync(entity).GetAwaiter().GetResult().As<AttendanceRemedyRulesDto>();
        }
    }
}
